package bb.lanxing.lib.devices.sprint.utils;

import bb.lanxing.lib.devices.api.ConnectionListener;
import bb.lanxing.lib.devices.api.DeviceManager;
import bb.lanxing.lib.devices.api.SmartDevice;
import bb.lanxing.lib.devices.sprint.SprintDeviceManager;
import bb.lanxing.lib.devices.sprint.XossDeviceController;
import kotlin.jvm.internal.DefaultConstructorMarker;

public final class XossDeviceConnector {
    private final boolean autoReset;
    private ConnectListener connectListener;
    private final ConnectionListener connectStateListener;
    private DeviceManager deviceManager;

    public interface ConnectListener {
        void onConnectFailed();

        void onConnected();

        void onConnecting();
    }

    public XossDeviceConnector() {
        this(false, 1, null);
    }

    public XossDeviceConnector(boolean z) {
        this.autoReset = z;
        this.connectStateListener = (smartDevice, i, i2) -> connectStateListener(this, smartDevice, i, i2);
    }

    public XossDeviceConnector(boolean z, int i, DefaultConstructorMarker defaultConstructorMarker) {
        this((i & 1) != 0 ? false : z);
    }

    public final boolean getAutoReset() {
        return this.autoReset;
    }

    public final DeviceManager getDeviceManager() {
        return this.deviceManager;
    }

    public final void setDeviceManager(DeviceManager deviceManager) {
        this.deviceManager = deviceManager;
    }

    public final ConnectListener getConnectListener() {
        return this.connectListener;
    }

    public final void setConnectListener(ConnectListener connectListener) {
        this.connectListener = connectListener;
    }

    public final ConnectionListener getConnectStateListener() {
        return this.connectStateListener;
    }


    public static final void connectStateListener(XossDeviceConnector deviceConnector, SmartDevice smartDevice, int i, int i2) {
//        Intrinsics.checkNotNullParameter(this$0, "this$0");
        if (i == 1) {
            ConnectListener connectListener = deviceConnector.connectListener;
            if (connectListener == null) {
                return;
            }
            connectListener.onConnecting();
        } else if (i == 2) {
            ConnectListener connectListener2 = deviceConnector.connectListener;
            if (connectListener2 != null) {
                connectListener2.onConnected();
            }
            deviceConnector.autoReset();
        } else if (i == 4) {
            ConnectListener connectListener3 = deviceConnector.connectListener;
            if (connectListener3 != null) {
                connectListener3.onConnectFailed();
            }
            deviceConnector.autoReset();
        }
    }

    public final void connect(DeviceManager deviceManager, String address, String name, int i, ConnectListener connectListener) {
//        Intrinsics.checkNotNullParameter(deviceManager, "deviceManager");
//        Intrinsics.checkNotNullParameter(address, "address");
//        Intrinsics.checkNotNullParameter(name, "name");
//        Intrinsics.checkNotNullParameter(connectListener, "connectListener");
        this.deviceManager = deviceManager;
        this.connectListener = connectListener;
        if (deviceManager instanceof SprintDeviceManager) {
            XossDeviceController xossDeviceController = ((SprintDeviceManager) deviceManager).getXossDeviceController(address, name, i);
            if (xossDeviceController != null) {
                if (!xossDeviceController.isConnected() || !xossDeviceController.isConnecting()) {
                    xossDeviceController.connect();
                }
                deviceManager.registerConnectionStateListener(this.connectStateListener);
                return;
            }
            connectListener.onConnectFailed();
            reset();
        }
    }

    public final void autoReset() {
        if (this.autoReset) {
            reset();
        }
    }

    public final void reset() {
        try {
            DeviceManager deviceManager = this.deviceManager;
            if (deviceManager != null) {
                deviceManager.unregisterConnectionStateListener(this.connectStateListener);
            }
            this.connectListener = null;
        } catch (Exception unused) {
        }
    }
}
